//
//  SortViewController.m
//  We_Travel
//
//  Created by lanou3g on 15/12/16.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "SortViewController.h"
#import "MapViewController.h"
#import "SiteManager.h"
#import "SiteRecomendModel.h"

@interface SortViewController ()<BMKMapViewDelegate,UITableViewDataSource,UITableViewDelegate>
//排序和传值用数组
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation SortViewController
- (void)viewWillAppear:(BOOL)animated{
    [_mapView viewWillAppear];
    _mapView.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigation];
    [self setupTableView];
    // Do any additional setup after loading the view from its nib.
}
//设置navigation
- (void)setupNavigation{
    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"拖放调整顺序";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
}
- (void)setupTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView setEditing:YES animated:YES];
     self.dataArray = [NSMutableArray arrayWithObjects:[SiteManager shareSiteManager].mornArray,[SiteManager shareSiteManager].afteArray,[SiteManager shareSiteManager].evenArray, nil];
}
- (void)rightBarButtonItemAction:(UIBarButtonItem *)sender{
   // self.sendDataBlock(self.dataArray);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- tableView datasource delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return [SiteManager shareSiteManager].mornArray.count;
    }else  if (section == 1) {
        return [SiteManager shareSiteManager].afteArray.count;
    }else{
        return [SiteManager shareSiteManager].evenArray.count;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
    if (indexPath.section == 0) {
        cell.textLabel.text = [[SiteManager shareSiteManager].mornArray[indexPath.row]name];
    }else  if (indexPath.section == 1) {
        cell.textLabel.text = [[SiteManager shareSiteManager].afteArray[indexPath.row]name];
    }else{
        cell.textLabel.text = [[SiteManager shareSiteManager].evenArray[indexPath.row]name];
    }
        return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 40)];
    headView.backgroundColor  = [UIColor whiteColor];
    UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 30, 30)];
  
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headerImageView.frame) + 10, 0, 50, 40)];
    [headView addSubview:headerImageView];
    [headView addSubview:timeLabel];
    if (section == 0) {
        timeLabel.text = @"上午";
        headerImageView.image = [UIImage imageNamed:@"morning.png"];
    }else if (section == 1) {
        timeLabel.text = @"中午";
        headerImageView.image = [UIImage imageNamed:@"afternoon.png"];
    }else if (section == 2) {
        timeLabel.text = @"晚上";
        headerImageView.image = [UIImage imageNamed:@"evening.png"];
    }
    return headView;
}
//移动
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
   
    if (sourceIndexPath.section != destinationIndexPath.section) {
        NSMutableArray *array1 = self.dataArray[sourceIndexPath.section];
        NSMutableArray *array2 = self.dataArray[destinationIndexPath.section];
        [array2 insertObject:array1[sourceIndexPath.row] atIndex:destinationIndexPath.row];
        [array1 removeObjectAtIndex:sourceIndexPath.row];
    }else{
        NSMutableArray *array = self.dataArray[sourceIndexPath.section];
       
        if (sourceIndexPath.row < destinationIndexPath.row) {
            [array insertObject:array[sourceIndexPath.row] atIndex:destinationIndexPath.row + 1];
            [array removeObjectAtIndex:sourceIndexPath.row];
        }else{
            [array insertObject:array[sourceIndexPath.row] atIndex:destinationIndexPath.row];
            [array removeObjectAtIndex:sourceIndexPath.row + 1];
        }
    }
    [SiteManager shareSiteManager].mornArray = self.dataArray[0];
    [SiteManager shareSiteManager].afteArray = self.dataArray[1];
    [SiteManager shareSiteManager].evenArray = self.dataArray[2];
    [tableView reloadData];
}

#pragma mark -- bmkMapViewDelegate
- (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate{
    MapViewController *mvc = [[MapViewController alloc]init];
    [self.navigationController showViewController:mvc sender:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
